package com.yuan.securitydemo.securtiy.utils;

import com.yuan.securitydemo.pojo.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;

public class SecurityUtils {
    private static SecurityContext context = SecurityContextHolder.getContext();
    private static Authentication authentication = context.getAuthentication();

    public static User getCurrentUser() {

        return (User)authentication.getPrincipal();
    }

    public static boolean isAuthenticated() {
        return authentication.isAuthenticated();
    }

    public static Collection<? extends GrantedAuthority> getAuthorities() {
        return authentication.getAuthorities();
    }

    public static SecurityContext getContext() {
        return context;
    }

    public static Authentication getAuthentication() {
        return authentication;
    }
}
